﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.RadioButtonList = function(element) {
    YUIAspNet.RadioButtonList.initializeBase(this, [element]);
}

YUIAspNet.RadioButtonList.prototype = {
    initialize: function() {
        YUIAspNet.RadioButtonList.callBaseMethod(this, 'initialize');
        // Add custom initialization here        
	 
	    //Sys.Application.add_init(Function.createDelegate(this, this.initializeRadioButtonList));
    },

    initializeYUIControl: function(sender, args) {
    
        var buttonGroup = new YAHOO.widget.ButtonGroup({  
                            id: this.get_element().id, 
                            name: this.get_element().id, 
                            container: this.get_element().id
                    }); 
	 
	    var buttons = this._getConvertedItems();
	    	 
	    buttonGroup.addButtons(buttons); 

	    var actualHandler = false;
	    var me = this;
    
	    
	    if(this._events)
	    {
	        var handler = this._events.getHandler("onCheckChange");
           
            actualHandler = function(args) { $get(me.get_StateFieldID()).value = args.newVal.get("value"); handler(me, args); };
	    }
	    else
	    {
    	     actualHandler = function(args) { $get(me.get_StateFieldID()).value = args.newVal.get("value"); };
	        
	    }
	    
	    buttonGroup.on("checkedButtonChange", function(event) { actualHandler({newVal: event.newValue}); }); 
        
        this._yuiControl = buttonGroup;
    },
    
    _getConvertedItems: function() {
        var items = [];
        
        for(var i=0; i<this.get_Items().length; i++) {
            items[i] = 
                { 
                    label: this.get_Items()[i].Label,
                    value: this.get_Items()[i].Value,
                    checked: this.get_Items()[i].Checked
                };
        }
        
        return items;
    },
    
    get_YUIControl: function() {
        return this._yuiControl;
    },
    
    dispose: function() {
        $clearHandlers(this.get_element());
    
        //Add custom dispose actions here
        YUIAspNet.RadioButtonList.callBaseMethod(this, 'dispose');
    },
    
    get_RadioButtonContainerID: function() {
        return this._radioButtonContainerID;
    },
    set_RadioButtonContainerID: function(radioButtonContainerID) {
        this._radioButtonContainerID = radioButtonContainerID;
    },
   
    get_Items: function() {
        return this._items;
    },
    set_Items: function(items) {
        this._items = items;
    },
        
    add_onCheckChange : function(handler) {
        this.get_events().addHandler("onCheckChange", handler);
    },
    remove_onCheckChange : function(handler) {
        this.get_events().removeHandler("onCheckChange", handler);
    }
}

YUIAspNet.RadioButtonList.registerClass('YUIAspNet.RadioButtonList', YUIAspNet.BaseControl);
